#pragma rtGlobals=1		// Use modern global access method.

Function gen_CheckWinExists(WindowNameStr) // generic function to check if window exists before renaming new window
	string WindowNameStr
	DoWindow $WindowNameStr
	if (v_flag>0)
		DoWindow/K $WindowNameStr
	endif	
	DoWindow/C $WindowNameStr
End	

Function gen_PrintWaveToNotebook(DataWv,HeaderStr,NotebookWinName)
// prints numerical wave contents to a notebook - conceived for Doug's input.dat file for chamber modelling.
	wave DataWv
	string HeaderStr // start the line with this before the actual data
	string NotebookWinName

	// check for existing notebook with that name
	string NotebookList = WinList(NotebookWinName,";","WIN:16") // list current notebooks of that name
	if (itemsinlist(NotebookList)==0)	// if notebook doesn't already exist
		newnotebook /n= $NotebookWinName /f=0 // create new notebook
	endif // else append to existing notebook	
	notebook $NotebookWinName selection={endoffile,endoffile} // set cursor to end of file position
	notebook $NotebookWinName text=HeaderStr
	variable i
	for (i=0;i<numpnts(DataWv);i+=1)
		notebook $NotebookWinName text="\t"+num2str(DataWv[i])
	endfor
	notebook $NotebookWinName text="\r"
	notebook $NotebookWinName font="consolas", fsize=12
	DoWindow/F $NotebookWinName // bring notebook to front
End

Menu "Data"
 "Bring DataBrowser to Front/8",/Q, CreateBrowser
End

Function gen_Loadunescape()
	// load waves from clipboard, unescaping ascii sequences for new line e.g. "\12" doesn't get treated as "\n".
	loadwave/J/V={"\t, "," $",1,8}/E=1/K=2 "clipboard"
End	

Function gen_KillOtherWaves(KeepList)
	wave/T KeepList
	variable i
	string WvStr=""
	DFRef CurrentDF=GetDataFolderDFR() 
	variable numWvs=countobjectsDFR(CurrentDF,1)
	for(i=numwvs-1;i>0;i-=1)
		WvStr=GetIndexedObjNameDFR(CurrentDF,1,i)
		FindValue/TEXT=WvStr/TXOP=2 KeepList
		if(V_value==-1) // =-1 if not found
			killwaves $WvStr
		endif
	endfor
End

Function KillWavesInCurrentDF(matchStr)
// kills all waves in current data folder that match <matchStr>
	string matchStr
	
	string WavesToDelete=wavelist(matchStr,";","")
	variable i
	variable NumDelWvs=itemsinlist(WavesToDelete)
	for(i=0;i<NumDelWvs;i+=1)
		KillWaves $stringfromlist(i,WavesToDelete)
	endfor	
End